const App = getApp();
import siteinfo from '../../siteinfo.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    avatarimg: '',
    nickname:'',
    pic:'',
    img:''
  },
    /** 获取昵称信息 */
    bindblur(e) {
this.data.nickname = e.detail.value
      console.log(e.detail.value)
  },
  /** 更换头像 */
  onChooseAvatar(e) {
      var that = this;
      wx.getFileSystemManager().readFile({
          filePath: e.detail.avatarUrl, //选择图片返回的相对路径 
          success: res => {
            wx.uploadFile({
              url: App.api_root + 'Wxapp/uploadimg',
              filePath: e.detail.avatarUrl,  
              name: 'file',
              formData: {
                wxapp_id: siteinfo.wxapp_id,
                token: wx.getStorageSync('token')
              },
              success: function (res) {
                 console.log(res);
                //打印
                var url=res.data;
                var  str2 =url.replace(/\"/g, "");
                var  str3 =str2.replace(/\\/g,'');
                // console.log(siteinfo.siteroot);
                var s_url=siteinfo.siteroot+str3
                console.log(s_url);
                var url =  "u"  + s_url.replace(/.*?\/u/gi,"");
                // console.log(url);
                that.setData({
                    img:siteinfo.siteroot+'web/'+url
                })
            }
          });
          }
      })
  },
  setBtnTap() {
      var that = this
      if (that.data.img.length == 0) {
          wx.showToast({
              title: '请上传头像!',
              icon: 'error',
              duration: 1500
          })
      } else if (that.data.nickname.length == 0) {
          wx.showToast({
              title: '请填写昵称!',
              icon: 'error',
              duration: 1500
          })
      } else {
       // 执行微信登录
       wx.login({
        success: function(res) {
          // 发送用户信息
          App._post_form('user/login', {
            code: res.code,
            nickName:that.data.nickname,
            avatarUrl:that.data.img,
          }, function(result) {
            console.log(result);
            // 记录token user_id
            wx.setStorageSync('token', result.data.token);
            wx.setStorageSync('user_id', result.data.user_id);
            // 跳转回原页面
          that.navigateBack();
          }, false, function() {
            wx.hideLoading();
          });
        }
      });
      }

  },
  toshiming:function(){
wx.navigateTo({
  url: '/pages/statement/xieyi',
})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
   // 设置navbar标题、颜色
  //  App.setNavigationBar();
this.setData({
  pic:siteinfo.siteroot
})

  },

  /**
   * 授权登录
   */
  getUserInfo: function(e) {
    let _this = this;
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (a) => {
        console.log(a);
    // let App = this;
    if (a.errMsg !== 'getUserProfile:ok') {
      return false;
    }
    // wx.showLoading({
    //   title: "正在登录",
    //   mask: true
    // });
    // 执行微信登录
    wx.login({
      success: function(res) {
        // 发送用户信息
        App._post_form('user/login', {
          code: res.code,
          user_info: a.rawData,
          encrypted_data: a.encryptedData,
          iv: a.iv,
          signature: a.signature,
        }, function(result) {
          // 记录token user_id
          wx.setStorageSync('token', result.data.token);
          wx.setStorageSync('user_id', result.data.user_id);
          // 跳转回原页面
          _this.navigateBack();
        }, false, function() {
          wx.hideLoading();
        });
      }
    });
  }
})
  },

  /**
   * 暂不登录
   */
  onNotLogin() {
    wx.redirectTo({
      url: '/pages/index/index'//页面路径
    })
    // let _this = this;
    // 跳转回原页面
    // _this.navigateBack();
  },

  /**
   * 授权成功 跳转回原页面
   */
  onNavigateBack() {
    wx.navigateBack();
  },
  /**
   * 授权成功 跳转回原页面
   */
  navigateBack: function() {
    wx.navigateBack();
    // let currentPage = wx.getStorageSync('currentPage');
    // wx.redirectTo({
    //   url: '/' + currentPage.route + '?' + util.urlEncode(currentPage.options)
    // });
  },

})